import { sectionNameList } from '@/views/baseManage/gcxSection/common'
const sectionName2value = sectionNameList.reduce((o, i) => !void (o[i.label] = i.value) && o, {})

// 项目池状态
export const proRangeStatusList = [
  { label: '永久移除', value: '-2', raw: { listClass: 'danger' } },
  { label: '作废', value: '-1', raw: { listClass: 'warning' } },
  { label: '正常', value: '1' },
  { label: '手动添加', value: '2', raw: { listClass: 'success' } }
]

// 检查单位(集团/集群)权限字段
export const rdcTypeAuthMap = {
  DEPT1: 'jt',
  DEPT2: 'jq'
}

// 一级考核指标 转 所属系统
export const firAssToBelongSysMap = {
  gc: 'ENGINEER',
  ds: 'DESIGN'
}

// 需要处理的 generationType: 检查(安排), 抽查
export const handleableGenType = ['CHECK', 'CHOOSE']

// 设计的管理动作编码
export const DS_GLDZ = 'DS-004-001'

// 重构检查项列表
export const refactorRltChkList = data => {
  if (!data?.length) return []

  // 分组
  const group = {}
  data.map(i => {
    const remark = i.remark || ''
    const key = `${i.sectionId}_${remark}`
    const item = group[key]
    if (item) return item.list.push(i)

    // 例：取出 '一标段(土建)' 里的 '一标段'
    const sectionName = (i.sectionName || '').split('(')[0] || ''
    const sectionSeq = sectionName ? sectionName2value[sectionName] || 0 : 0
    // 用标段和频次排序。remark 值形如 '第1次检查'
    const seq = `${sectionSeq}_${remark}`

    const displayName = `${i.sectionName || i.displayName} ${remark}`
    group[key] = { key, seq, displayName, list: [i] }
  })

  // 排序
  const groupList = Object.values(group).sort((a, b) => a.seq.localeCompare(b.seq, 'zh-Hans-CN', { numeric: true }))

  // 设计的管理动作改为树形
  groupList.map(r => {
    const list = r.list
    const item = list.find(i => i.chkitemCode === DS_GLDZ)
    if (!item) return

    item.isGldz = true
    // 管理动作的启用取决于有子项启用
    item.isEnable = false

    const children = list.filter(i => i.chkitemCode.includes(`${DS_GLDZ}-`))
    if (!children.length) return

    children.map(i => {
      i.isGldzChild = true
      if (!item.isEnable && i.isEnable) item.isEnable = true
    })
    item.children = children

    // 从当前级过滤掉子项
    r.list = list.filter(i => !i.chkitemCode.includes(`${DS_GLDZ}-`))
  })

  return groupList
}
